﻿using System;
using System.Collections.Generic;
using LZX.MEditor.Enum;
using LZX.MEditor.LZXStatic;
using LZX.MEditor.MScriptableObject;
using LZX.MEditor.Window.Item;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace LZX.Main.Editor.Window
{
    public class RunTimeInfoWindow:EditorWindow
    {
        public VisualTreeAsset uxml;
        public StyleSheet uss;
        public VisualElement root_selectlabel;
        public DropdownField dropdown_loadstatus;
        public VisualElement root_infoItem;
        [MenuItem("Tools/LZX/Windows/资源分析面板")]
        public static void ShowWindow()
        {
            EditorWindow.GetWindow<RunTimeInfoWindow>().minSize = new Vector2(800, 600);
        }

        private void OnEnable()
        {
            LoadStatuHelper.RegiterLoadAsset(OnLoadAsset);
            LoadStatuHelper.RegiterUnLoadAsset(OnUnLoadAsset);
            LoadStatuHelper.RegiterUnLoadBundle(OnUnLoadBundle);
            EditorApplication.playModeStateChanged += OnPlayModeChanged;
        }
        private void OnPlayModeChanged(PlayModeStateChange state)
        {
            switch (state)
            {
                case PlayModeStateChange.ExitingPlayMode:
                    LoadStatuHelper.LoadedAssets.Clear();
                    LoadStatuHelper.LoadedBundles.Clear();
                    foreach (var kv in BundleItems)
                    {
                        kv.Value.SetLoadBG(false);
                        foreach (var kvasset in kv.Value.Assets)
                        {
                            kvasset.Value.SetLoadBG(false);
                        }
                    }
                    break;
                case PlayModeStateChange.EnteredPlayMode:
                    LoadStatuHelper.Statu =(LoadStatu)LZXEditorResources.GetSetting().LoadStatu;
                    break;
            }
        }
        private void CreateGUI()
        {
            var root = uxml.CloneTree();
            root.styleSheets.Add(uss);
            var container = root.Q<VisualElement>("container");
            container.styleSheets.Add(uss);
            root_selectlabel = root.Q<VisualElement>("selectlabelcontainer");
            root.Q<Button>("btn_add").clicked += OnAddSelectLabel;
            dropdown_loadstatus = root.Q<DropdownField>("dropdown_loadstatu");
            dropdown_loadstatus.index = (int)LZXEditorResources.GetSetting().LoadStatu;
            dropdown_loadstatus.RegisterValueChangedCallback(OnLoadStatuChanged);
            root_infoItem = root.Q<VisualElement>("scrollview_runtimeinfo");
            InitWindow();
            rootVisualElement.Add(container);
        }
        private Dictionary<string, RunTimeBundle> BundleItems = new Dictionary<string, RunTimeBundle>();
        private void InitWindow()
        {
            foreach (var kv in LZXEditorResources.GetVersionController().Bundles)
            {
                RunTimeBundle item = new RunTimeBundle(kv.Value,root_infoItem);
                BundleItems.Add(kv.Value.Name+LZXEditorResources.GetSetting().BundleEx, item);
            }
            foreach (var kv in LoadStatuHelper.LoadedBundles)
            {
                if (BundleItems.TryGetValue(kv.Key, out var item))
                {
                    item.SetLoadBG(true);
                    item.Count.text = kv.Value.Count.ToString();
                }
            }
        }
        private void OnLoadStatuChanged(ChangeEvent<string> evt)
        {
            LoadStatuHelper.Statu = (LoadStatu)dropdown_loadstatus.index;
            LZXEditorResources.GetSetting().LoadStatu = dropdown_loadstatus.index;
        }
        private void OnAddSelectLabel()
        {
            throw new NotImplementedException();
        }
        private void OnUnLoadBundle(string bundleName, bool UnloadUnloadedAssets)
        {
            if (BundleItems.TryGetValue(bundleName, out var item))
            {
                item.SetLoadBG(false);
                item.Count.text = "0";
                item.OnUnLoadAllAsset(UnloadUnloadedAssets);
            }
        }
        private void OnLoadAsset(string bundleName,string assetName)
        {
            if (BundleItems.TryGetValue(bundleName, out var item))
            {
                item.SetLoadBG(true);
                item.Group.text = LoadStatuHelper.LoadedBundles[bundleName].Count.ToString();
                item.OnLoadAsset(assetName);
            }
        }
        private void OnUnLoadAsset(string bundleName, string assetName)
        {
            if (BundleItems.TryGetValue(bundleName, out var item))
            {
                item.Count.text = LoadStatuHelper.LoadedBundles[bundleName].Count.ToString();
                item.OnUnLoadAsset(assetName);
            }
        }
        private void OnDestroy()
        {
            LoadStatuHelper.UnRegiterLoadAsset(OnLoadAsset);
            LoadStatuHelper.UnRegiterUnLoadAsset(OnUnLoadAsset);
            LoadStatuHelper.UnRegiterUnLoadBundle(OnUnLoadBundle);
            EditorApplication.playModeStateChanged -= OnPlayModeChanged;
        }
    }
}